package com.manage.servlet;

import com.manage.pojo.Building;
import com.manage.pojo.ResultData;
import com.manage.service.BuildingService;
import com.manage.service.impl.BuildingServiceImpl;
import com.manage.utils.FileUploadUtils;
import com.manage.utils.JsonUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/building")
public class BuildingServlet extends  BaseServlet{
    //分页展示楼栋
    public ResultData all(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        //1.获取数据
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");

        //2.处理请求
        BuildingService buildingService=new BuildingServiceImpl();
        ResultData resultData = buildingService.selectByPage(page, limit);


        //3.响应请求
        return resultData;
    }
    //删除一个楼栋
    public ResultData del(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        //1.获取数据
        String bid = request.getParameter("bid");


        //2.处理请求
        BuildingService buildingService=new BuildingServiceImpl();
        ResultData resultData = buildingService.removeOneByBid(bid);


        //3.响应请求
        return resultData;
    }
    //    缩略图上传
    public ResultData upload(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

        //保存到数据库中的名称
        String fileName = FileUploadUtils.uploadFile(req, "file");
        //3、做出响应
        return new ResultData(0,"成功",fileName);
    }
    //添加一个楼栋
    public ResultData add(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        //1.获取数据
        String building = request.getParameter("building");
        Building building1 = JsonUtils.jsonToObject(building, Building.class);

        //2.处理请求
        BuildingService buildingService=new BuildingServiceImpl();
        ResultData resultData = buildingService.addBuilding(building1);


        //3.响应请求
        return resultData;
    }
}
